layui.use([ 'form', 'element'], function() {
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    var exnr = layer.load();
    var params = sdk.getURLVar('call');
    if (!params) {
        checkLogin();
        // consoleMain();
    } else {
        firstReq();
    }
    // 首次登录要调用makeLink
    function firstReq() {
        $.ajax({
            type: 'post',
            data: {
                call: params 
            },
            url: sdk.companyIp + 'zxw/company/baohan/makeLink',
            success: function(d) {
                checkLogin();
            }
        })
    };

    // 验证checkLogin
    function checkLogin() {
        $.ajax({
            type: 'post',
            url: sdk.companyIp + 'zxw/company/baohan/checkLogin',
            success: function(d) {
                var urlStr =
                    'https://ggzy.jz.gov.cn:8606/jinzhou-servicePlatform/api/forward?comMark=' +
                    d.comMark + '&signstr=' + d.signstr + '&time=' + d.time + '&token=' +
                    d.token;
                if (d.isLogin) {
                    $('#iframeBody').attr('src', urlStr);
                    $('.xy-iframe').removeClass('noShow');
                    $('.noLoginIframe').addClass('noShow');
                    $.ajax({
                        type: 'post',
                        url: sdk.companyIp + 'zxw/company/login/getLoginFlag',
                        dataType: 'json',
                        success: function(userinfo) {
                            layer.close(exnr);
                            if (userinfo.loginFlag == 'true') {
                                $('#isLogin').removeClass('noShow');
                                $('#noLogin').addClass('noShow');
                                $('.bankName').text(userinfo.userName);
                                $('.bankName').attr('data-val', userinfo.userType);
                                sessionStorage.setItem('baohan_info', d.isLogin);
                            } else {
                                $('.xy-iframe').addClass('noShow');
                                $('.noLoginIframe').removeClass('noShow');
                                $('#isLogin').addClass('noShow');
                                $('#noLogin').removeClass('noShow');
                                sessionStorage.setItem('baohan_info', "");
                            }
                        },
                        error: function() {
                            layer.close(exnr);
                        }
                    })
                } else {
                    $('.xy-iframe').addClass('noShow');
                    $('.noLoginIframe').removeClass('noShow');
                    $('#isLogin').addClass('noShow');
                    $('#noLogin').removeClass('noShow');
                    sessionStorage.setItem('baohan_info', "");
                    layer.close(exnr);
                }
            },
            error: function() {
                layer.close(exnr);
            }
        })
    }


    // 加载弹窗
    $('.baohan_body').click(function() {
        var type = $(this).attr('data-type');
        if (type == 'bh') {
            $.ajax({
                url: 'modify_menu.html',
                dataType: 'html',
                success: function(d) {
                    $('body').append(d);
                    layer.open({
                        type: 1,
                        title: '操 作 选 择',
                        shadeClose: true,
                        maxWidth: 400,
                        content: $('#nhnvve_menu'),
                        success: function(layero, index) {
                            $('.baohan_edit').click(function() {
                                var way = $(this).text().trim();
                                if (way != '下载操作演示') {
                                    var flag = sdk.myBrowser();
                                    if (flag) {
                                        window.open(
                                            'http://ggzy.jz.gov.cn:8605/TPBidder/memberLogin'
                                        );
                                    } else {
                                        layer.msg('该操作仅限于IE浏览器 !', {
                                            icon: 2,
                                            time: 1500
                                        });
                                    }
                                }
                                layer.close(index);
                            })
                        },
                    })
                }
            })
        }
    });

    // 角色身份选择
    element.on('nav(loginUser)', function(elem) {
        var code = $(elem.context).attr('code');
        if (code) {
            loginSvkc(code);
        }
    });

    // 改变验证码
    function changeValidateImg() {
        $('#imgYzm').attr('src', '').attr('src', sdk.companyIp +
            'zxw/company/login/getRandomValidateCode?r=' + Math.random());
        form.val("formVcyk", {
            validateCode: ''
        });
    };

    // 渲染登录页面
    function loginSvkc(typeId) {
        if ($('#layerVcyk').length == 0) {
            $.ajax({
                url: 'login2.html',
                dataType: 'html',
                success: function(html) {
                    $('body').append(html);
                    loginMain(typeId);
                }
            });
        } else {
            loginMain(typeId)
        }
    };
    // 个人信息
    function consoleMain() {
        $.ajax({
            type: 'post',
            url: sdk.companyIp + 'zxw/company/login/getLoginFlag',
            dataType: 'json',
            success: function(userinfo) {
                if (userinfo.loginFlag == 'true') {
                    $('#isLogin').removeClass('noShow');
                    $('#noLogin').addClass('noShow');
                    $('.bankName').text(userinfo.userName);
                    $('.bankName').attr('data-val', userinfo.userType);
                } else {
                    $('#noLogin').removeClass('noShow');
                    $('#isLogin').addClass('noShow');
                }
            },
            error: function(err) {
                layer.msg('系统错误,请联系管理员!', {
                    icon: 5
                });
                $('#noLogin').removeClass('noShow');
                $('#isLogin').addClass('noShow');
            }
        });
    }

    // 登录渲染角色类型
    function loginMain(typeId) {
        if (typeId == 'qy') {
            $('#bklwxo').text('企业登录');
            $('#zwrkoe').hide();
        } else if (typeId == 'jg') {
            $('#bklwxo').text('机构登录');
            $('#zwrkoe').hide();
        } else if (typeId == 'zf') {
            $('#bklwxo').text('监管登录');
            $('#zwrkoe').hide();
        }
        layer.open({
            type: 1,
            title: false,
            shadeClose: true,
            content: $('#layerVcyk'),
            success: function(layero, index) {
                changeValidateImg();
                $('#imgYzm').click(function() {
                    changeValidateImg()
                });
                form.on('submit(formGohk)', function(d) {
                    var rjzpIndex = layer.load(2);
                    var data = d.field;
                    var acc = data.account;
                    var psw = data.password;
                    data.account = btoa(acc);
                    data.password = btoa(psw);
                    setTimeout(function() {
                        layer.close(rjzpIndex);
                    }, 5000)
                    $.ajax({
                        type: 'post',
                        data: data,
                        url: sdk.companyIp +
                            'zxw/company/login/loginCheck',
                        success: function(d) {
                            layer.close(rjzpIndex);
                            consoleMain();
                            changeValidateImg();
                            if (d.code == '00') {
                                layer.msg(d.msg, {
                                    icon: 6,
                                    closeBtn: 1,
                                    time: 3000
                                });
                                if (sdk.user[d.typeId]) {
                                    location.href = sdk.baseIP + sdk.user[d
                                        .typeId].path;
                                }
                                layer.close(index);
                            } else {
                                layer.msg(d.msg, {
                                    icon: 2,
                                    closeBtn: 1,
                                    time: 3000
                                });
                            }
                        },
                        error: function(err) {
                            layer.msg('系统错误,请联系管理员!', {
                                icon: 5
                            });
                        }
                    });
                });
            },
            end: function() {
                form.val("formVcyk", {
                    account: '',
                    password: '',
                    validateCode: ''
                });
            }
        });
    };


    // 跳转到工作台
    $('.bankName').click(function() {
        var typeId = $(this).attr('data-val');
        if (sdk.user[typeId]) {
            location.href = sdk.baseIP + sdk.user[typeId].path;
        }
    });
    // 签退
    $('.logoutBtn').click(function() {
        var layer = layui.layer;
        layer.confirm('你确定要退出吗?', {
            icon: 0,
            title: '提示'
        }, function(index) {
            layer.close(index);
            sessionStorage.setItem('baohan_info', '');
            location.href = sdk.baseIP + 'zxw/company/login/loginout';
        });
    });
});
